package com.jic.member.impl.power;

import java.lang.Long;
import java.util.Date;
import java.util.List;


import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.enums.RedisKey;
import com.jic.enums.RedisTime;
import com.jic.member.entity.MememberUser;
import com.jic.member.entity.power.MememberRecipientInformation;
import com.jic.member.mapper.MememberUserMapper;
import com.jic.member.mapper.power.MememberRecipientInformationMapper;
import com.jic.member.service.power.MememberRecipientInformationService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 会员收货信息表
 * @author : 
 * @email : 
 * @since : 2020-05-27 02:55:00
 * @version : v1.0.0
 */
@Service
public class MememberRecipientInformationServiceImpl implements MememberRecipientInformationService {

    @Resource
    private MememberRecipientInformationMapper mememberRecipientInformationMapper;
    @Resource
    RedisClientTemplate redisClientTemplate;
    @Autowired
    private MememberUserMapper mememberUserMapper;

    @Override
    public long insert(MememberRecipientInformation mememberRecipientInformation) {
        long flag = mememberRecipientInformationMapper.insert(mememberRecipientInformation);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return mememberRecipientInformationMapper.delete(id);
    }

    @Override
    public long update(MememberRecipientInformation mememberRecipientInformation) {
        return mememberRecipientInformationMapper.update(mememberRecipientInformation);
    }

    @Override
    public MememberRecipientInformation queryByPrimaryKey(Long id){
        return mememberRecipientInformationMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MememberRecipientInformation> query(MememberRecipientInformation mememberRecipientInformation){
        return mememberRecipientInformationMapper.query(mememberRecipientInformation);
    }

    @Override
    public PageResult<MememberRecipientInformation> queryPage(MememberRecipientInformation mememberRecipientInformation, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        mememberRecipientInformation.setDeleteFlag(0);
        List<MememberRecipientInformation> list = mememberRecipientInformationMapper.queryPage(startIndex, pageQuery.getLimit(), mememberRecipientInformation);
        long count = mememberRecipientInformationMapper.queryPageCount(mememberRecipientInformation);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult updateInfo(MememberRecipientInformation mememberRecipientInformation) {
        long flag;
        AdminOperate adminOperate = mememberRecipientInformation.getAdminOperate();
        String userStr = redisClientTemplate.get(RedisKey.H5_LOGIN_TOKEN_.getName() + mememberRecipientInformation.getToken());
        if (StringUtils.isEmpty(userStr)){
            return RestResult.error("4000","登录失效");
        };
        MememberUser mMember = JSONArray.parseObject(userStr, MememberUser.class);
        if (mememberRecipientInformation.getIsDefault() == 1){
            MememberRecipientInformation query = new MememberRecipientInformation();
            query.setMemberUserId(mMember.getId());
            query.setIsDefault(1);
            List<MememberRecipientInformation> result = mememberRecipientInformationMapper.query(query);
            if (CollectionUtils.isNotEmpty(result)){
                MememberRecipientInformation update = new MememberRecipientInformation();
                update.setId(result.get(0).getId());
                update.setIsDefault(0);
                mememberRecipientInformationMapper.update(update);
            }
        }
        mememberRecipientInformation.setMemberUserId(mMember.getId());
        if (null != mememberRecipientInformation.getId()){
            flag = mememberRecipientInformationMapper.update(mememberRecipientInformation);
        }else {
            mememberRecipientInformation.setDeleteFlag(0);
            flag = mememberRecipientInformationMapper.insert(mememberRecipientInformation);
            if (flag > 0) {
                //会员姓名为空更新会员手机号码、和姓名
                if (StringUtils.isEmpty(mMember.getName())) {
                    mMember.setName(mememberRecipientInformation.getConsigneeName());
                    mMember.setPhone(mememberRecipientInformation.getPhone());
                    mMember.setUpdateTime(new Date());
                    mememberUserMapper.update(mMember);
                    redisClientTemplate.setex(RedisKey.H5_LOGIN_TOKEN_.getName() + mememberRecipientInformation.getToken(),
                            Integer.valueOf(RedisTime.H5_LOGIN_TIME_.getName().toString()), JSONArray.toJSONString(mMember));
                }
            }
        }
        return RestResult.success(flag);
    }

    @Override
    public RestResult deleteInfo(MememberRecipientInformation addressRequest) {
        if (null !=addressRequest.getId()){
            mememberRecipientInformationMapper.delete(addressRequest.getId());
        }
        return RestResult.success("操作成功");
    }

}